// import Cookies from 'js-cookie'
import { cloneDeep } from 'lodash'
import { MEUN_TREE, COMMON_PATH } from '@/common/menu'
const auth = {
  namespaced: true,
  state: {
    activeResourceCode: '',
    menuTree: [],
    routeList: []
  },
  mutations: {
    SET_MEUN_TREE: (state, menuTree = []) => {
      state.menuTree = menuTree
    },
    SET_ACTIVE_RESOURCE_CODE: (state, activeCode = '') => {
      state.activeResourceCode = activeCode
    },
    SET_ROUTE_LIST: (state, routeList = []) => {
      state.routeList = routeList
    }
  },
  actions: {
    setMenuTree({ commit }, payload = {}) {
      var { resources = [] } = payload
      return new Promise((resolve) => {
        var menuTree = calculateRouteMenuTree(cloneDeep(MEUN_TREE), cloneDeep(resources))
        var routeList = dimensionReductionTree(cloneDeep(menuTree)).concat(cloneDeep(COMMON_PATH))
        var routeListS = cloneDeep([]).concat(routeList)
        commit('SET_MEUN_TREE', menuTree)
        commit('SET_ROUTE_LIST', routeListS)
        resolve(cloneDeep({ routeList: routeListS, menuTree }))
      })
    },
    setActiveResourceCode({ state, commit }, { $route }) {
      var { name: routeName = '' } = $route
      var findNode = state.routeList.find(item => item.routeName === routeName) || {}
      var activeCode = findNode['resourceCode'] || ''
      return new Promise((resolve) => {
        commit('SET_ACTIVE_RESOURCE_CODE', activeCode)
        resolve()
      })
    },
    checkAuthPathIsPass({ state, commit }, { $route }) {
      var { name: routeName = '' } = $route
      var findNode = state.routeList.find(item => item.routeName === routeName)
      // var activeCode = findNode['resourceCode'] || ''
      return new Promise((resolve, reject) => {
        if (findNode) {
          resolve(cloneDeep(findNode))
        } else {
          reject()
        }
      })
    }
  }
}

// 计算一级路由树
function calculateRouteMenuTree(frontMenuTree = [], resourceTree = []) {
  var resourceTreeList = dimensionReductionTree(resourceTree)
  return cloneDeep(frontMenuTree).map(node => {
    var findRootNode = resourceTreeList.find(item => item.resourceCode === node.resourceCode)
    if (!findRootNode) {
      return null
    }
    node = Object.assign(node, { resourceName: findRootNode.resourceName, resourceId: findRootNode.resourceId })
    if (node && node.children && node.children.length) {
      node.children = node.children.map(cNode => {
        var findSecondNode = resourceTreeList.find(itemC => itemC.resourceCode === cNode.resourceCode)
        if (!findSecondNode) {
          return null
        }
        cNode = Object.assign(cNode, { resourceName: findSecondNode.resourceName, resourceId: findSecondNode.resourceId })
        return cNode
      }).filter(item => !!item)
    }
    return node
  }).filter(item => !!item)
}

// 树平摊
function dimensionReductionTree(treeArr = []) {
  var nodeArr = []
  var treeList = cloneDeep(treeArr)
  function fn(tree) {
    var nodeNew = cloneDeep(tree)
    if (tree.children && tree.children.length) {
      tree.children.forEach(item => {
        fn(item)
      })
    }
    if (nodeNew.children) {
      delete nodeNew.children
    }
    nodeArr.push(nodeNew)
  }
  treeList.forEach(tree => {
    fn(tree)
  })
  return nodeArr
}

// 生成权限树
// func
export default auth
